package com.fuluwa.domain.product.service;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

import com.fuluwa.domain.product.model.Product;
import com.fuluwa.infra.jms.NotifyMessageListener;
import com.fuluwa.infra.lucene.LuceneService;
import com.fuluwa.infra.lucene.LuceneServiceImpl;
import com.fuluwa.infra.web.Result;

public class FileUploadListener implements MessageListener {

	private static Logger logger = LoggerFactory.getLogger(NotifyMessageListener.class);

	@Override
	@Transactional
	public void onMessage(Message message) {
		// TODO Auto-generated method stub
		System.out.println("Should process file:"+message);
		MapMessage mapMessage = (MapMessage) message;
		try {
	/*		long id = mapMessage.getLong("fileInfoId");
			FileInfo info=new FileInfo(id);
			info=info.load();
			TikaManager tikaManager = new TikaManager(new File(info.getPhiscalPath()));
			
			System.out.println(tikaManager.getContent());
			
			info.updateIndexed();*/
			long id = mapMessage.getLong("id");
			Product product=new Product(id);
			product=product.load();
			LuceneService luceneService=new LuceneServiceImpl();
			Result result = luceneService.indexAddOrUpdadate(product.getUnquieId(), product.toSolrMap());
			if(result.isSuccess()) {
				product.updateIndexed();
				if(!product.getFileInfo().isIndexed())
				product.getFileInfo().updateIndexed();
			}
		} catch (Exception e) {
			logger.error("JMS process Error", e);
		}
	}

}
